package calificaciones;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import sistemas.sam.R;
import sistemas.sam.R.layout;
import sistemas.sam.R.menu;
import sistemas.sam.ValidacionAsistencia;
import tutorias.conexion.Bean;

import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AlumnosAltaCalif extends Activity implements OnItemClickListener {
	String idM,idG,idU;
	JSONArray jArray=null;
	String result = null;
	String []nombre;
	String []matricula;
	String dirr;
	InputStream is = null;
	StringBuilder sb = null;
	String php;
	String nom = "",matri="";
	ProgressDialog dialog;
	
	private ConectarSam mAuthTask = null;
	ListView lista;
	public List<String> Lalumnos =new ArrayList<String>(); 
	public List<String> Lmatriculas =new ArrayList<String>(); 
	ArrayAdapter<String> adaptado2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alumnos_alta_calif);
		
		
		Bundle bundle=getIntent().getExtras();
		idM=""+bundle.getString("idM");
		idG=""+bundle.getString("idG");
		idU=""+bundle.getString("idU");
		
		
		Bean bean=new Bean();
		dirr=bean.getDireccion();
		php="Alumnos";
		

		
		
		mAuthTask=new ConectarSam();
		mAuthTask.execute((Void)null);
		
		

		
		adaptado2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Lalumnos);
	lista=(ListView) findViewById(R.id.alumnos);
		//adaptado2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		lista.setAdapter(adaptado2);
		
		
		lista.setOnItemClickListener(this);
		
		//listEmployeeDetails=(ListView)findViewById(R.id.listaAlumnos);
	//listEmployeeDetails.setAdapter((ListAdapter) Lalumnos);
	}

	
	public void llenar(String [] n,String [] m){
		Lalumnos.clear();
		Lmatriculas.clear();
		
		for(int i=0;i<n.length;i++){
			Lalumnos.add(""+n[i]);
		}
		
		
		for(int i=0;i<m.length;i++){
			Lmatriculas.add(""+m[i]);
		}
		adaptado2.notifyDataSetChanged();
	}

	private void abrirDashboard() {
	//	selectAsesoria.putExtra("materias", materias);
		//startActivity(selectAsesoria);
		llenar(nombre,matricula);
		
	}
	public class ConectarSam extends AsyncTask<Void, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Void... params) {
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			// http post
			try {
				HttpClient httpclient = new DefaultHttpClient();
				HttpPost httppost = new HttpPost(dirr+php+
						".php?unidadID="+idU+"&materiaID="+idM+"&grupoID="+idG);
				Log.v("log_tag", dirr+php+
						".php?unidadID="+idU+"&materiaID="+idM+"&grupoID="+idG);
				httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
				HttpResponse response = httpclient.execute(httppost);
				HttpEntity entity = response.getEntity();
				is = entity.getContent();
			} catch (Exception e) {
				Log.e("log_tag", " "
						+ e.toString());
			}
			try {
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(is, "iso-8859-1"), 8);
				sb = new StringBuilder();
				sb.append(reader.readLine() + "\n");

				String line = "0";
				while ((line = reader.readLine()) != null) {
					sb.append(line + "\n");
				}
				is.close();
				result = sb.toString();
				Log.v(" obtenido", result);
			} catch (Exception e) {
				Log.e("log_tag",
						"Error convirtiendo datos recibidos" + e.toString());
			}
			try {
				jArray = new JSONArray(result);
				JSONObject json_data = null;
				nombre =new String[jArray.length()]; 
				matricula=new String[jArray.length()];
				for (int i = 0; i < jArray.length(); i++) {
					json_data = jArray.getJSONObject(i);
					nombre[i]= json_data.getString("nombre");	
				matricula[i]=json_data.getString("matricula");	
					Log.d("alumno ", "paso  "+nombre[i]);
					Log.d("mate_id", "paso  "+matricula[i]);
				}
			} catch (JSONException e1) {
				Log.e("JSON ERR", e1.getMessage());
			} catch (ParseException e1) {
				e1.printStackTrace();
			}
			return true;
		}
		@Override
		protected void onPostExecute(final Boolean success) {
			mAuthTask = null;
			//showProgress(false);
			Log.v("login_resultado", success.toString());
			if (success) {
				
				abrirDashboard();

			} else {
				
			}
		}

		@Override
		protected void onCancelled() {
			mAuthTask = null;
			//showProgress(false);
		}
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int location, long arg3) {
		// TODO Auto-generated method stub
		nom=Lalumnos.get(location);
		matri=Lmatriculas.get(location);
		
		Intent i=new Intent(this,ValidacionAsistencia.class);
			Bundle bundle = new Bundle();
			bundle.putString("nombreA",nom);
			bundle.putString("matriA",matri);
			bundle.putString("IDm", idM);
	        bundle.putString("IDu", idU);
	        bundle.putString("IDg", idG); 
			i.putExtras(bundle);
			startActivity(i);
		
	}
	
	
	
}
